<?php
if (isset($_SESSION['admin-login'])) {
	if (isset($_SESSION['status-produk-new'])) {
		echo "<p>".$_SESSION['status-produk-new']."</p>";
		unset($_SESSION['status-produk-new']);
	}
	?>
	<html><body>
	<form enctype="multipart/form-data" action="<?php echo site()."/".$param[0]."/produk-new-action" ?>" method="post" onsubmit="return validateProduk()">
		<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
		<input type="hidden" name="id" />
		Gambar :<br><input name="image" type="file" /><br>
		Nama :<br><input type="text" name="nama" /><br>
		Harga :<br>Rp <input type="text" name="harga" /><br>
		Deskripsi :<br><textarea name="deskripsi"></textarea><br>
		Kategori :<br><input type="text" name="kategori" /><br>
		<input type="submit" name="post" id="post" value="Post" disabled="disabled" /><br>
		<a href="<?php echo site()."/".$param[0]."/produk" ?>">Cancel</a>
	</form>
	<script type="text/javascript">
	document.getElementById("post").removeAttribute("disabled");
	function validateProduk() {
		var bulat = /^\d+$/;
		var body = document.getElementsByTagName("body")[0];
		var form = document.getElementsByTagName("form")[0];
		var image = form["image"].value.split('.').pop().toLowerCase();
		var nama = form["nama"].value;
		var harga = form["harga"].value;
		var deskripsi = form["deskripsi"].value;
		var kategori = form["kategori"].value;
		var status = document.getElementsByTagName("p")[0] || document.createElement("p");
		if (nama.length == 0 || harga.length == 0 || deskripsi.length == 0 || kategori.length == 0 || image.length == 0) {
			status.innerHTML = "Semua input harus diisi";
			body.insertBefore(status,form);
		} else if (image != "jpg" && image != "jpeg") {
			status.innerHTML = "Tipe gambar hanya boleh jpg, atau jpeg";
			body.insertBefore(status,form);
		} else if (!bulat.test(harga)) {
			status.innerHTML = "Harga harus angka";
			body.insertBefore(status,form);
		} else {
			if (document.getElementsByTagName('p')[0])
				body.removeChild(document.getElementsByTagName('p')[0]);
			return true;
		}
		return false;
	}
	</script>
	</body></html>
	<?php
} else {
	$_SESSION['status-login'] = "Log in terlebih dahulu untuk melanjutkan";
	header("Location: ".site()."/".$param[0]."/login");
}
?>